<?
//Maximum number of options that a product may have
if(!isset($gOptions)) $gOptions = 2;
$_SESSION["ajax"] = false;

//Setup some arrays to hold items, discounts, and handling fees.
if(!$_SESSION["tempCart"] || $_GET['clearCart'] == 1  || $_POST['clearCart'] == 1) {
	$_SESSION["tempCart"] = array();
	$_SESSION["gDiscounts"] = array();
	$_SESSION["gCartInfo"] = array();
	$_SESSION["gFees"] = array();

	}

//Remove an item from an array.
if($_GET['clearItem'] || $_POST['clearItem']) {
	$key = $_GET['clearItem'];
	if($_POST['clearItem']) $key = $_POST['clearItem'];
	$itemTotal = $_SESSION["tempCart"][$key]['quantity'] * $_SESSION["tempCart"][$key]['price'];
	$_SESSION["gCartInfo"]['weight'] -= $_SESSION["tempCart"][$key]['weight'] * $_SESSION["tempCart"][$key]['quantity'];
	$_SESSION["gCartInfo"]['discounts'][$_SESSION["tempCart"][$key]['discount']]['quantity'] -= $_SESSION["tempCart"][$key]['quantity'];
	$_SESSION["gCartInfo"]['discounts'][$_SESSION["tempCart"][$key]['discount']]['total'] -= $itemTotal;
	$_SESSION["gCartInfo"]['quantity'] -= $_SESSION["tempCart"][$key]['quantity'];
	$_SESSION["gCartInfo"]['subtotal'] -= $itemTotal;
	$_SESSION["ajax"] = true;
	unset($_SESSION["tempCart"][$key]);
	return;
	}

//Increment the item.
if($_GET['increment'] || $_POST['increment']) {
	$key = $_GET['increment'];
	if($_POST['increment']) $key = $_POST['increment'];
	$_SESSION["tempCart"][$key]['quantity'] += 1;
	$_SESSION["gCartInfo"]['quantity'] += 1;
	$_SESSION["gCartInfo"]['subtotal'] += $_SESSION["tempCart"][$key]['price'];
	$_SESSION["gCartInfo"]['weight'] += $_SESSION["tempCart"][$key]['weight'];
	$_SESSION["gCartInfo"]['discounts'][$_SESSION["tempCart"][$key]['discount']]['quantity'] += 1;
	$_SESSION["gCartInfo"]['discounts'][$_SESSION["tempCart"][$key]['discount']]['total'] += $_SESSION["tempCart"][$key]['price'];
	$_SESSION["ajax"] = true;
	return;
	}
	
//Decrement the item.
if($_GET['decrement'] || $_POST['decrement']) {
		$key = $_GET['decrement'];
		if($_POST['decrement']) $key = $_POST['decrement'];
		$_SESSION["tempCart"][$key]['quantity'] -= 1;
		$_SESSION["gCartInfo"]['quantity'] -= 1;
		$_SESSION["gCartInfo"]['subtotal'] -= $_SESSION["tempCart"][$key]['price'];
		$_SESSION["gCartInfo"]['weight'] -= $_SESSION["tempCart"][$key]['weight'];
		$_SESSION["gCartInfo"]['discounts'][$_SESSION["tempCart"][$key]['discount']]['quantity'] -= 1;
		$_SESSION["gCartInfo"]['discounts'][$_SESSION["tempCart"][$key]['discount']]['total'] -= $_SESSION["tempCart"][$key]['price'];
		if($_SESSION["tempCart"][$key]['quantity'] <= 0)unset($_SESSION["tempCart"][$key]);
		$_SESSION["ajax"] = true;
		return;
}

//Shipping Information
//Decrement the item.
if($_POST['shipZip']) {
		$_SESSION['gCartInfo']['shipZip'] = $_POST['shipZip'];
		$_SESSION['gCartInfo']['shipMethod'] = $_POST['shipMethod'];
		return;
}

//Initialize the Token
if(!$_SESSION["shoptoken"]) $_SESSION["shoptoken"] = 0;

//Validate Input
if(is_numeric($_POST['gPrice']) && $_POST['gPrice'] > 0 && is_numeric($_POST['gQuantity']) && $_POST['gQuantity'] >0 && !strpos($_POST['gQuantity'],'.'))
	$validInput = true;
	else
		$validInput = false;

//Check to make sure this is not a resubmission of already submitted data.
if($_POST['token'] > $_SESSION["shoptoken"] && $validInput){
	
	//Add the options to the name and see if additional fees need to be charged.
	if(isset($_POST['gOption1'])) {
		$tempOptions = '';
		for($i = 1; $i <= $gOptions; $i++) {
			$gOpt = 'gOption'.$i;
			if(!isset($_POST[$gOpt])) break;
			$addedFee = strpos($_POST[$gOpt], '+');
			if ($addedFee) $tempOptions .= substr($_POST[$gOpt],0,$addedFee);
				else $tempOptions .= $_POST[$gOpt];
			$addedFee += 1;
			//echo $addedFee . '|';
			$addedFee = substr($_POST[$gOpt],$addedFee);
			$_POST['gPrice'] += $addedFee;
			}
		$alias = $_POST['gSKU'];
		$_POST['gSKU'] .= str_replace(' ', '', $tempOptions);
		$_POST['gName'] = $tempOptions . ' ' . $_POST['gName'];
		} else $alias = $_POST['gSKU'];
		
	//if the item is already in the cart, update the quantity only, if not add it to the cart.
	if($_SESSION["tempCart"][$_POST['gSKU']]) {
		$_SESSION["tempCart"][$_POST['gSKU']]['quantity'] += substr($_POST['gQuantity'],0,10);
		}
	else {
		$_SESSION["tempCart"][$_POST['gSKU']] = array('name' => $_POST['gName'], 
													  'description' => $_POST['gDescription'], 
													  'quantity' => substr($_POST['gQuantity'],0,10), 
													  'price' => substr($_POST['gPrice'],0,10), 
													  'sku' => $_POST['gSKU'],
													  'alias' => $alias,
													  'discount' => $_POST['gDiscount'],
													  'taxstatus' => $_POST['gTaxExempt'],
													  'freeship' => $_POST['gFreeShip'],
													  'weight' => $_POST['gWeight']);
		}
	if($_POST["alias"]) $_SESSION["tempCart"][$_POST['gSKU']]['alias'] = $_POST["alias"];
	$_SESSION["gCartInfo"]['quantity'] += $_POST['gQuantity'];	
	$_SESSION["gCartInfo"]['subtotal'] += $_POST['gQuantity']*$_POST['gPrice'];
	$_SESSION["gCartInfo"]['weight'] += $_POST['gQuantity']*$_POST['gWeight'];
	$_SESSION["shoptoken"] = $_POST['token'];
	$_SESSION["gCartInfo"]['discounts'][$_POST['gDiscount']]['quantity'] += $_POST['gQuantity'];
	$_SESSION["gCartInfo"]['discounts'][$_POST['gDiscount']]['total'] += $_POST['gQuantity'] * $_POST['gPrice'];
}
return;